package com.lh.glorypavilion.service.aliyunvod.controller.admin;

import com.lh.glorypavilion.exception.glorypavilionEx;
import com.lh.glorypavilion.result.R;
import com.lh.glorypavilion.result.ResultCode;
import com.lh.glorypavilion.service.aliyunvod.service.VideoService;
import com.lh.glorypavilion.util.ExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Api(description="阿里云视频点播")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/vod/media")
@Slf4j
public class MediaController {

    @Autowired
    private VideoService videoService;

    @ApiOperation("上传视频")
    @PostMapping("upload")
    public R uploadVideo(
            @ApiParam(value = "视频文件", required = true)
            @RequestParam("file") MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String videoId = videoService.uploadVideo(inputStream, originalFilename);
            return R.ok().message("视频上传成功").data("videoId", videoId);
        } catch (IOException e) {
            log.error(ExceptionUtils.getMessage(e));
            throw  new glorypavilionEx(ResultCode.VIDEO_UPLOAD_TOMCAT_ERROR);
        }
    }


    @ApiOperation("id删除视频")
    @DeleteMapping("remove/{vodId}")
    public R removeVideo(
            @ApiParam(name="vodId", value="阿里云视频id", required = true)
            @PathVariable("vodId") String vodId){

        try {
            videoService.removeVideo(vodId);
            return R.ok().message("视频删除成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new glorypavilionEx(ResultCode.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }

    @ApiOperation("批量删除视频列表")
    @DeleteMapping("removeList")
    public R removeList(
            @ApiParam(value = "视频列表", required = true)
            @RequestBody List<String> videoIdList){

        try {
            videoService.removeVideoByIdList(videoIdList);
            return R.ok().message("视频删除成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new glorypavilionEx(ResultCode.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }
}